package com.vhackandroid.bremen.quizbox.data;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class FragenDB {

	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;

	public FragenDB(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public boolean insertFrage(Fragen frage) {

		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_FRAGE_ID,
				String.valueOf(frage.getId()));
		values.put(MySQLiteHelper.COLUMN_FRAGE, frage.getFrage());
		values.put(MySQLiteHelper.COLUMN_ANTWORT0, frage.getAntwort0());
		values.put(MySQLiteHelper.COLUMN_ANTWORT1, frage.getAntwort1());
		values.put(MySQLiteHelper.COLUMN_ANTWORT2, frage.getAntwort2());
		values.put(MySQLiteHelper.COLUMN_ANTWORT3, frage.getAntwort3());
		values.put(MySQLiteHelper.COLUMN_ANTWORT_RIGHT,
				frage.getRichtigeAntwort());
		values.put(MySQLiteHelper.COLUMN_KATEGORIE, frage.getKategorie());
		values.put(MySQLiteHelper.COLUMN_DIFFICULTY,
				String.valueOf(frage.getDifficulty()));

		long insertId = database.insert(MySQLiteHelper.TABLE_FRAGEN, null,
				values);

		if (insertId == -1) {
			return false;
		} else {
			return true;
		}
	}

	public List<Fragen> getAllFragen() {
		List<Fragen> fragen = new ArrayList<Fragen>();

		Cursor cursor = database.rawQuery("SELECT * FROM "
				+ MySQLiteHelper.TABLE_FRAGEN, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Fragen frage = cursorToFragen(cursor);
			fragen.add(frage);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return fragen;
	}

	private Fragen cursorToFragen(Cursor cursor) {
		int id = Integer.valueOf(cursor.getString(cursor
				.getColumnIndex(MySQLiteHelper.COLUMN_FRAGE_ID)));
		String frage = cursor.getString(cursor
				.getColumnIndex(MySQLiteHelper.COLUMN_FRAGE));
		String antwort0 = cursor.getString(cursor
				.getColumnIndex(MySQLiteHelper.COLUMN_ANTWORT0));
		String antwort1 = cursor.getString(cursor
				.getColumnIndex(MySQLiteHelper.COLUMN_ANTWORT1));
		String antwort2 = cursor.getString(cursor
				.getColumnIndex(MySQLiteHelper.COLUMN_ANTWORT2));
		String antwort3 = cursor.getString(cursor
				.getColumnIndex(MySQLiteHelper.COLUMN_ANTWORT3));
		int richtigeAntwort = Integer.valueOf(cursor.getString(cursor
				.getColumnIndex(MySQLiteHelper.COLUMN_ANTWORT_RIGHT)));
		String kategorie = cursor.getString(cursor
				.getColumnIndex(MySQLiteHelper.COLUMN_KATEGORIE));
		int difficulty = Integer.valueOf(cursor.getString(cursor
				.getColumnIndex(MySQLiteHelper.COLUMN_DIFFICULTY)));
		return new Fragen(id, frage, antwort0, antwort1, antwort2, antwort3,
				richtigeAntwort, kategorie, difficulty);
	}

	public void clearFragen() {
		int i = database.delete(MySQLiteHelper.TABLE_FRAGEN, null, null);
	}

}